<?php 


if (!in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1')))
{
//  die('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}

require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', true);
sfContext::createInstance($configuration);

$databaseManager = new sfDatabaseManager($configuration);

//mailer to send a message to users that have a task that are due today

//grab all tasks that are due today
$date = date("Y-m-d", time());
$tasks = TaskPeer::retrieveTasksByDate($date);
$count = 0;

//for each late task
foreach($tasks as $index=>$task)
{
	$count = $count + 1;
	
	//retrieve data
	$user_id = $task['user_id'];
	$user = UserPeer::retrieveById($user_id);
	$name = $task['name'];
	$group_id = $task['group_id'];
	$group = GroupPeer::retrieveById($group_id);
	$group_name = $group->getName();
	$email = $user->getEmail();
	
	//create message
	$subject = 'Task Due Today';
	
	$body = '';
	$body = 'You have a Task that is Due. <br/>';
	$body = $body.'Group Name: '.$group_name.'<br/>';
	$body = $body.'Task Name: '.$name.'<br/>';
	$body = $body.'Due Date: '.$date.'<br/>';
	$body = $body.'<a href="http://www.finalsclub.org/groups/show/id/'.$group->getId().'"> view the task.</a>';
	$body = $body.'<hr/> FinalsClub copyright 2008';
	
	//mail message
	$mail = new ezcMailComposer();
	$mail->from = new ezcMailAddress('admin@finalsclub.org', 'Finals Club');
	$mail->addTo(new ezcMailAddress($user->getEmail(), $user->getFirstName().' '.$user->getLastName()));
	$mail->subject = $subject;
 	$mail->plainText = "You have a task that is due at finalsclub.org.";
 	$mail->htmlText = $body;
 	$mail->build();
 	
	$transport = new ezcMailMtaTransport();
	$transport->send($mail);
}

echo('mailed '.$count.' message(s).');